package com.tuniu.agents.common.message;

import com.tuniu.agents.message.StreamMessage;
import com.tuniu.agents.util.JsonUtil;
import lombok.Data;

@Data
public class BaseAgentResp implements StreamMessage {

    private int code;// Success code

    private String message;// "Success" or error message

    private String questionId;// uuid, each Q&A has a unique uuid (also used for subsequent history retrieval)

    private String sessionId;// session id

    private int doneFlag;// 0, output in progress; 1 output complete (at this point, the full content needs to be returned, including refer, think, answer, productData)

    private Refer refer;// Reference data

    private Think think;// Thought content

    private String answer;// Main text

    private ProductData productData;// Only displayed when doneFlag is 1

    public static BaseAgentResp getSuccessMsg(String questionId, String sessionId){
        BaseAgentResp baseAgentResp = new BaseAgentResp();
        baseAgentResp.setCode(710000);
        baseAgentResp.setMessage("OK");
        baseAgentResp.setQuestionId(questionId);
        baseAgentResp.setSessionId(sessionId);
        return baseAgentResp;
    }
    public static BaseAgentResp getErrorMsg(int code,String message,String questionId,String sessionId){
        BaseAgentResp baseAgentResp = new BaseAgentResp();
        baseAgentResp.setCode(code);
        baseAgentResp.setMessage(message);
        baseAgentResp.setQuestionId(questionId);
        baseAgentResp.setSessionId(sessionId);
        baseAgentResp.setDoneFlag(1);// Failure, mark complete
        return baseAgentResp;
    }

    /**
     * Send error message
     * @param code Error code
     * @param message Error message
     */
    public static String sendErrorMsg(int code, String message, String questionId, String sessionId) {
        BaseAgentResp errorMsg = getErrorMsg(code, message,questionId,sessionId);
        return errorMsg.toEventString();
    }

    /**
     * Send reference information
     * @param refer
     * @param questionId
     */
    public static String sendReferMsg(Refer refer,String questionId, String sessionId) {
        BaseAgentResp referMsg = getSuccessMsg(questionId,sessionId);
        referMsg.setRefer(refer);
        return referMsg.toEventString();
    }

    /**
     * Send thought information
     * @param questionId
     * @param sessionId
     */
    public static String sendThinkMsg(String questionId, String sessionId,Think think) {
        BaseAgentResp thinkMsg = getSuccessMsg(questionId,sessionId);
        thinkMsg.setThink(think);
        return thinkMsg.toEventString();
    }

    /**
     * Send main text information
     * @param questionId
     * @param sessionId
     */
    public static String sendAnswerMsg(String questionId, String sessionId,String answer) {
        BaseAgentResp thinkMsg = getSuccessMsg(questionId,sessionId);
        thinkMsg.setAnswer(answer);
        return thinkMsg.toEventString();
    }

    /**
     * Send success information
     * @param questionId
     * @param sessionId
     * @param refer Complete refer
     * @param think Complete think
     * @return
     */
    public static String sendFinishMsg( String questionId, String sessionId,Refer refer,Think think ,String answer, ProductData productData) {
        BaseAgentResp successMsg = getSuccessMsg(questionId, sessionId);
        successMsg.doneFlag = 1;
        successMsg.setRefer(refer);
        successMsg.setThink(think);
        successMsg.setAnswer(answer);
        successMsg.setProductData(productData);
        return successMsg.toEventString();
    }

    /**
     * Builder static inner class
     */
    public static class Builder {
        private int code;
        private String message;
        private String questionId;
        private String sessionId;
        private int doneFlag;
        private Refer refer;
        private Think think;
        private String answer;
        private ProductData productData;

        public Builder() {}

        public Builder code(int code) {
            this.code = code;
            return this;
        }

        public Builder message(String message) {
            this.message = message;
            return this;
        }

        public Builder questionId(String questionId) {
            this.questionId = questionId;
            return this;
        }

        public Builder sessionId(String sessionId) {
            this.sessionId = sessionId;
            return this;
        }

        public Builder doneFlag(int doneFlag) {
            this.doneFlag = doneFlag;
            return this;
        }

        public Builder refer(Refer refer) {
            this.refer = refer;
            return this;
        }

        public Builder think(Think think) {
            this.think = think;
            return this;
        }

        public Builder answer(String answer) {
            this.answer = answer;
            return this;
        }

        public Builder productData(ProductData productData) {
            this.productData = productData;
            return this;
        }

        public BaseAgentResp build() {
            BaseAgentResp baseAgentResp = new BaseAgentResp();
            baseAgentResp.setCode(code);
            baseAgentResp.setMessage(message);
            baseAgentResp.setQuestionId(questionId);
            baseAgentResp.setSessionId(sessionId);
            baseAgentResp.setDoneFlag(doneFlag);
            baseAgentResp.setRefer(refer);
            baseAgentResp.setThink(think);
            baseAgentResp.setAnswer(answer);
            baseAgentResp.setProductData(productData);
            return baseAgentResp;
        }
    }

    @Override
    public String toEventString() {
        return  JsonUtil.toJson(this);
//        return "data: " + new Gson().toJson(this) + "\n\n";
    }


}
